package locacao.integracao.dao.pstPostgreSQL;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;

import locacao.integracao.dao.ItemVeiculoDAO;
import locacao.negocio.to.ItemVeiculoTO;

public class ItemVeiculoPgsqlDAO implements ItemVeiculoDAO {

    private ArrayList<ItemVeiculoTO> itemVeiculos;

    public ItemVeiculoPgsqlDAO(){
	   setItemVeiculos(new ArrayList<ItemVeiculoTO>());
    }


    public void addOrUpdateItemVeiculo(ItemVeiculoTO itemVeiculo) {
    	Connection conn = null;
    	CallableStatement spManterItemVeiculo = null;

    	try {        	
          String sql = "{call locacao.manter_itemVeiculo(?,?,?,?)}";
          conn = ConnectionRentACar.getInstance().getConnection();
          
          spManterItemVeiculo = conn.prepareCall(sql);
          
          spManterItemVeiculo.setInt(1, itemVeiculo.getIdLocacao());
          spManterItemVeiculo.setInt(2, itemVeiculo.getIdVeiculo());
          spManterItemVeiculo.setInt(3, itemVeiculo.getQuantidadeDiaria());
          spManterItemVeiculo.setInt(4, (int)itemVeiculo.getValorDiaria());
          
          spManterItemVeiculo.execute();
        } catch (Exception e) {
          System.out.println(e.getMessage());
        } finally {
          try {
            if (spManterItemVeiculo != null)
            	spManterItemVeiculo.close();

          } catch (SQLException e) {
            System.out.println(e.getMessage());
          }
        }
    }

    /**
     *

    /**
     * @param itemVeiculo
     */
    public void excludeItemVeiculo( ItemVeiculoTO itemVeiculo )  {
    	Connection conn = null;
    	CallableStatement spDeleteItemVeiculo = null;

    	try {        	
          String sql = "{call locacao.delete_itemVeiculo(?,?)}";
          conn = ConnectionRentACar.getInstance().getConnection();
          
          spDeleteItemVeiculo = conn.prepareCall(sql);
          
          spDeleteItemVeiculo.setInt(1, itemVeiculo.getIdLocacao());         
          spDeleteItemVeiculo.setInt(2, itemVeiculo.getIdVeiculo());  
          
          spDeleteItemVeiculo.execute();
        } catch (Exception e) {
          System.out.println(e.getMessage());
        } finally {
          try {
            if (spDeleteItemVeiculo != null)
            	spDeleteItemVeiculo.close();

          } catch (SQLException e) {
            System.out.println(e.getMessage());
          }
        }
    }


  
    /**
     * @return
     */
    public String[] findItemVeiculoById(Integer idLocacao, Integer idVeiculo){
    	 Connection conn = null;
    	 Statement stmt = null;
    	 ResultSet rSet = null;
    	 String[] info = null;

    	 String sql = "SELECT  idlocacao, idveiculo, qtddiaria, vlrdiaria ";
    	 sql += " FROM locacao.itemVeiculo WHERE idlocacao =  " + idLocacao + " idveiculo = " + idVeiculo;

    	 try {
    		 conn = ConnectionRentACar.getInstance().getConnection();
    		 stmt = conn.createStatement();
    		 rSet = stmt.executeQuery(sql);

    		 if (rSet != null) {    			 
    			 while (rSet.next()) {
    				 
    				info = new String[itemVeiculos.size()];
    	    		   info[0] = rSet.getInt("idlocacao") 
    	       		   + " - " + rSet.getInt("idveiculo") 
    	       		   + " - " + rSet.getInt("qtddiaria")
    	       		   + " - " + rSet.getFloat("vlrdiaria");  		   
    			 }    			 
    		 }
			 return info;
			 
    	 } catch (Exception e) {
    		 System.out.println(e.getMessage());
    		 return null;
    	 } finally {
    		 try {
    			 if (rSet != null)
    				 rSet.close();
    			 if (stmt != null)
    				 stmt.close();
    		 } catch (SQLException e) {
    			 System.out.println(e.getMessage());
    		 }
    	 }
    }

    
    /**
     * @return
     */
    public String[] getInfoItemVeiculos()
    {
        ItemVeiculoTO itemVeiculo;
        itemVeiculos = getItemVeiculos();
        String[] info = new String[itemVeiculos.size()];
        Iterator iteratorItemVeiculos;
        iteratorItemVeiculos = itemVeiculos.iterator();
        int i=0;
        while ( iteratorItemVeiculos.hasNext() )
        {
            itemVeiculo = (ItemVeiculoTO) iteratorItemVeiculos.next();
            info[i] = itemVeiculo.getIdLocacao()
            		+ " - " + itemVeiculo.getIdVeiculo()
            		+ " - " + itemVeiculo.getQuantidadeDiaria()
            		+ " - " + itemVeiculo.getValorDiaria();
            i++;
        }
        return info;
    }


    /**
     * @return
     */
    public ArrayList<ItemVeiculoTO> getItemVeiculos() {
    	 Connection conn = null;
    	 Statement stmt = null;
    	 ResultSet rSet = null;
    	 ItemVeiculoTO itemVeiculo = null;

    	 String sql = "SELECT  iditemVeiculo, fabricante, modelo, ano, qtdportas, cor, placa, combustivel, status ";
    	 sql += " FROM locacao.itemVeiculo ORDER BY iditemVeiculo";

    	 try {
    		 conn = ConnectionRentACar.getInstance().getConnection();
    		 stmt = conn.createStatement();
    		 rSet = stmt.executeQuery(sql);

    		 if (rSet != null) {
    			 itemVeiculos = new ArrayList();
    			 while (rSet.next()) {
    				 
    				 itemVeiculo = new ItemVeiculoTO();
    				 
    				 itemVeiculo.setIdLocacao(rSet.getInt("idlocacao"));
    				 itemVeiculo.setIdVeiculo(rSet.getInt("idveiculo"));
    				 itemVeiculo.setQuantidadeDiaria(rSet.getInt("qtddiaria"));
    				 itemVeiculo.setValorDiaria(rSet.getFloat("vlrdiaria"));
    				 
    				 itemVeiculos.add(itemVeiculo);
    			 }
    		 }
    	 } catch (Exception e) {
    		 System.out.println(e.getMessage());
    	 } finally {
    		 try {
    			 if (rSet != null)
    				 rSet.close();
    			 if (stmt != null)
    				 stmt.close();
    		 } catch (SQLException e) {
    			 System.out.println(e.getMessage());
    		 }
    	 }
    	 return itemVeiculos;
    }

    private void setItemVeiculos(ArrayList<ItemVeiculoTO> itemVeiculos) {
    	this.itemVeiculos = itemVeiculos;
    }
    
}
